﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Mgh.Data.Sql;
using Jewelry.BussinessLayer;
using Jewelry.Entities;
using System.Data;
using System.Globalization;

public partial class Pages_AEditOrder : Mgh.UI.Sql.PageBase
{
    #region properties

    protected CultureInfo myInfo;

    protected string OrderID
    {
        get { return Request.QueryString["oid"] == null ? string.Empty : Request.QueryString["oid"]; }
    }

    private DataSources.Jw_Tbl_OrderDataTable tbOrder
    {
        get { return ViewState["order"] as DataSources.Jw_Tbl_OrderDataTable; }
        set { ViewState["order"] = value; }
    }

    private DataSources.Jw_Vw_OrderDetailDataTable tbDetail
    {
        get { return ViewState["detail"] as DataSources.Jw_Vw_OrderDetailDataTable; }
        set 
        { 
            ViewState["detail"] = value;
            int tt = 0;
            foreach (DataSources.Jw_Vw_OrderDetailRow rw in value)
            {
                tt += rw.IsThanhTienNull() ? 0 : rw.ThanhTien;
            }
            Label21.Text = tt.ToString("$#,#", myInfo);
            //Label21.Text = String.Format("0,0", tt);
        }
    }

    #endregion

    #region events

    override protected void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        myInfo = CultureInfo.InvariantCulture;
        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(OrderID))
                this.bindData();
        }
    }

    protected void clickUpdateItem(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow rw = gv.Rows[e.RowIndex];
        tbDetail[rw.DataItemIndex].TrangThai = (rw.Cells[5].Controls[1] as DropDownList).SelectedValue;
    }

    protected void clickUpdateOrder(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if (!prepairData())
            {
                cval.IsValid = false;
                cval.Text = "Data have error";
                return;
            }

            OrderBLL bn = new OrderBLL(getCurrentConnect());
            if (!bn.updateFullOrder(tbOrder[0], tbDetail))
            {
                cval.IsValid = false;
                cval.Text = bn.getMessage();
            }
        }
    }

    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            (e.Row.Cells[5].Controls[1]as DropDownList).SelectedValue = tbDetail[e.Row.DataItemIndex].TrangThai;
            //(DataSources.Jw_Vw_OrderDetailRow)(e.Row.DataItem as DataRow);
        }
    }

    protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gv.PageIndex = e.NewPageIndex;
        gv.DataSource = tbDetail;
        gv.DataBind();
    }

    #endregion

    #region functions

    private bool prepairData()
    {
        if (tbOrder.Count > 0)
        {
            tbOrder[0].TrangThai = dlStatus.SelectedValue;
            if (dlStatus.SelectedValue.Equals("Finish"))
            {
                tbOrder[0].KetThuc = System.DateTime.Now;
            }
            return true;
        }
        return false;
    }

    private void bindData()
    {
        OrderBLL bn = new OrderBLL(getCurrentConnect());
        DataSources.Jw_Tbl_OrderDataTable tb1 = new DataSources.Jw_Tbl_OrderDataTable();
        DataSources.Jw_Vw_OrderDetailDataTable tb2 = new DataSources.Jw_Vw_OrderDetailDataTable();

        if (bn.getFullOrder(OrderID, ref tb1, ref tb2))
        {
            tbOrder = tb1;
            tbDetail = tb2;
            if (tbOrder.Count > 0)
            {
                Label1.Text = tbOrder[0].OrID;
                Label2.Text = tbOrder[0].Username;
                Label3.Text = tbOrder[0].IsNgayTaoNull() ? string.Empty : tbOrder[0].NgayTao.ToString("dd/MM/yyyy");
                Label4.Text = tbOrder[0].IsKetThucNull() ? string.Empty : tbOrder[0].KetThuc.ToString("dd/MM/yyyy");
                dlStatus.SelectedValue = tbOrder[0].IsTrangThaiNull() ? string.Empty : tbOrder[0].TrangThai;
            }

            if (tbDetail.Count > 0)
            {

                gv.DataSource = tbDetail;
                gv.DataBind();
            }
        }
        else
        {
            cval.IsValid = false;
            cval.Text = bn.getMessage();
        }

    }

    #endregion

}